﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CarlosAg.ExcelXmlWriter;
using App.Core.DB;
using System.IO;
using WebScrapper.Entities;

namespace WebScrapper.Helpers
{
    public class DataExporter
    {
        public static String ToExcel(List<Company> companies, bool addColumnHeader, String outputFileName)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets.Add("Sheet1");
            if (companies != null && companies.Count > 0)
            {
                if (addColumnHeader)
                {
                    WorksheetRow headerRow = sheet.Table.Rows.Add();
                    addHeaderRow(headerRow);
                }

                for (int i = 0; i < companies.Count; i++)
                {
                    WorksheetRow row = sheet.Table.Rows.Add();
                    Company company = companies[i];
                    addDataRow(row, company);
                }
            }

            //row.Cells.Add("Hello World");        

            //String fileName = String.Format("{0}.xls", System.Guid.NewGuid().ToString());
            String filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OutputExcel"); //HttpContext.Current.Server.MapPath(AppConstants.TEMP_DIRECTORY);            
            if (!System.IO.Directory.Exists(filePath))
                System.IO.Directory.CreateDirectory(filePath);

            filePath = System.IO.Path.Combine(filePath, outputFileName);

            book.Save(filePath);

            return filePath;
        }
        private static void addHeaderRow(WorksheetRow row)
        {            
            //for (int i = 0; i < dt.Columns.Count; i++)
            //{
            //    if (ignoredFieldIndexes == null || ignoredFieldIndexes.Count == 0 || ignoredFieldIndexes.SingleOrDefault(P => P == i) == null)
            //        row.Cells.Add(dt.Columns[i].ColumnName);
            //}
            row.Cells.Add("Company Name");
            row.Cells.Add("Industry");
            row.Cells.Add("Postal Code");
            row.Cells.Add("Locality");
            row.Cells.Add("Stree Address");
            row.Cells.Add("Telephone");
            row.Cells.Add("Email");
            row.Cells.Add("Details");
            row.Cells.Add("Details Url");
        }
        private static void addDataRow(WorksheetRow row, Company company)
        {
            //for (int i = 0; i < dtRow.ItemArray.Length; i++)// object data in dtRow.ItemArray)
            //{
            //    if (ignoredFieldIndexes == null || ignoredFieldIndexes.Count == 0 || ignoredFieldIndexes.SingleOrDefault(P => P == i) == null)
            //        row.Cells.Add(ConvertToString(dtRow[i]));                
            //}
            row.Cells.Add(company.Name);
            row.Cells.Add(company.Category);
            row.Cells.Add(company.PostalCode);
            row.Cells.Add(company.Locality);
            row.Cells.Add(company.StreetAddress);
            row.Cells.Add(company.Telephone);
            row.Cells.Add(company.Email);
            row.Cells.Add(company.Details);
            row.Cells.Add(company.DetailsUrl);
        }
    }
}
